package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class,
		protocol = "dubbo")
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

	@Autowired
	private CheckItemMapper checkItemMapper;

	@Override
	public void add(CheckItem checkItem) {
		checkItemMapper.add(checkItem);

	}


	@Override
	public PageResult pageQuery(QueryPageBean queryPageBean) {

		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		//分页助手
		PageHelper.startPage(currentPage, pageSize);
		//调用mapper的查询方法
		Page<CheckItem> page = checkItemMapper.findPage(queryString);
		//
		PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

		return pageResult;
	}

	@Override
	public void delete(int id) {
		//查询该条记录是否与其他表有关联
		long count = checkItemMapper.findIsRelation(id);
		if (count > 0) {
			//中间表有管理数据
			new RuntimeException();
		}else {
			//没有关联可以删除
			checkItemMapper.delete(id);
		}


	}

	@Override
	public CheckItem queryItemById(Integer id) {

		CheckItem item = checkItemMapper.queryItemById(id);

		return item;
	}

	@Override
	public void edit(CheckItem checkItem) {

		checkItemMapper.edit(checkItem);

	}

	@Override
	public List<CheckItem> findAll() {
		List<CheckItem> checkItemList = checkItemMapper.findAll();

		return checkItemList;
	}

	@Override
	public List<Integer> findCheckitemsIdsByCheckgroupId(Integer id) {
		List<Integer> ids = checkItemMapper.findCheckitemsIdsByCheckgroupId(id);

		return ids;
	}

}